<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <title>单页管理列表 Content List</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, content-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>
    <style type="text/css">
        .layui-btn-dropdown{position:relative;display:inline-block}
        .layui-btn-dropdown .layui-dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;float:left;display:none;margin:2px 0 0;padding:5px 0;min-width:120px;border:1px solid #ccc;border:1px solid rgba(0,0,0,.15);border-radius:4px;background-color:#fff;background-clip:padding-box;-webkit-box-shadow:0 6px 12px rgba(0,0,0,.175);box-shadow:0 6px 12px rgba(0,0,0,.175);list-style:none;text-align:left;font-size:.875rem;-webkit-background-clip:padding-box}
        .layui-btn-dropdown .layui-dropdown-menu>li>a{clear:both;display:block;padding:3px 15px;color:#333;white-space:nowrap;font-weight:400;line-height:1.42857143}
        .layui-btn-dropdown .layui-dropdown-menu>li>a:focus,.layui-btn-dropdown .layui-dropdown-menu>li>a:hover{background-color:#f0f0f0;text-decoration:none}
        .layui-btn-dropdown .layui-dropdown-menu .divider{overflow:hidden;margin:9px 0;height:1px;background-color:#e5e5e5}
        table tbody td{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
        .layui-btn-container .layui-btn-group .layui-icon{position: static !important;padding: 0 10px !important;}
    </style>
    <script th:inline="javascript">
        var code = [[${module.code}]];
        var cols = eval([[${cols}]]);
    </script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-form layui-card">
                <div class="layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">

                        <div class="layui-inline" style="float: left;">
                            <select  name="layui-form-field" id="layui-form-field" class="form-control input-sm" style="width:auto;">
                                <option value="" >选择搜索条件</option>
                                <option th:each="sfd:${searchedFields}" th:value="${sfd.name}" th:text="${sfd.title}"></option>
                            </select>
                        </div>
                        <div class="layui-inline" style="float: left;">
                            <input class="layui-input" name="keyword" id="layui-form-keyword" autocomplete="off"/>
                        </div>
                        <div style="float: left;margin-left: 2px;">
                            <select  name="layui-form-group" id="layui-form-group" lay-filter="layui-form-group-filter" class="form-control input-sm" style="width:250px;">
                                <option value="" >选择内容分组</option>
                                <option th:each="g:${groups}" th:value="${g.id}" th:text="${g.name}"></option>
                            </select>
                        </div>
                        <div class="layui-btn-container" style="float: left" >
                            <div class="layui-btn-group">
                                <button class="layui-btn layui-btn-primary" data-type="addGroup" style="padding: 0;"><i class="layui-icon layui-icon-add-1"></i></button>
                                <button class="layui-btn layui-btn-primary" data-type="removeGroup" style="padding: 0;"><i class="layui-icon layui-icon-close"></i></button>
                                <button class="layui-btn layuiadmin-btn-list" data-type="search">搜索</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="content-list-table" lay-filter="content-list-filter"></table>

                    <script type="text/html" id="content-list-table-operate-bar">
                        {{# if(d.id > 0 ){ }}
                        <a th:if="${editAction}" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a th:if="${removeAction}" class="layui-btn layui-btn-xs layui-btn-primary" lay-event="remove">删除</a>
                        {{# } }}
                    </script>

                    <script type="text/html" id="content-list-table-switch-state-tpl">
                        <input type="checkbox" name="state" lay-skin="switch" lay-filter="content-list-table-state"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.state == 1 ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="content-list-table-toolbar">
                        <button th:if="${addAction}" class="layui-btn layui-btn-sm" lay-event="add">
                            添加[[${module.name}]]
                        </button>
                    </script>

                    <blockquote class="layui-elem-quote">
                        <div style="margin-bottom: 10px;">
                            本功能适用于 <span style="color: #1E9FFF"><b>[[${module.name}]]</b></span> 模型，请先 <span style="color: #009688"><b>添加</b></span> 内容分组；
                        </div>
                    </blockquote>
                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , table = layui.table;

        var fieldName = $('#layui-form-field');
        var keyword = $('#layui-form-keyword');
        var formgroup = $('#layui-form-group')

        var tableIns = table.render({
            elem: '#content-list-table'
            , url: '/api/content/page?code='+code
            , id: 'id'
            , toolbar: '#content-list-table-toolbar'
            , cols: cols
        });

        form.on("switch(content-list-table-state)",function(obj){
            //var json = JSON.parse(decodeURIComponent($(this).data('json')));
            layer.load();
            var stateInt = obj.elem.checked ? 1 : 0;
            $.ajax({
                url: '/api/content/state',
                method: 'post',
                data: {id : this.value , state : stateInt},
                dataType: 'JSON',
                success: function (res) {
                    layer.closeAll();
                    if (res.code == '0') {
                        layer.msg('数据修改完成!', {icon: 1,time: 2000});
                    } else {
                        layer.msg('ERROR:数据修改错误!', {icon: 2,time: 2000});
                    }
                },
                error: function (err) {
                    layer.closeAll();
                    layer.msg('数据修改错误', {icon: 2,time: 2000});
                }
            });
            //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);

            //json = table.clearCacheKey(json);
            //console.log(json); //当前行数据
        });

        //头工具栏事件
        table.on('toolbar(content-list-filter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    var index = layer.open({
                        type: 2
                        , title: '添加单页内容'
                        , content: '/cms/data/content/add?code='+code
                        , maxmin: false
                        , area: ['500px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-page-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                console.log(field);

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/content/add',
                                    method: 'post',
                                    data: field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            layer.msg(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    layer.full(index);
                    break;
            }
            ;
        });
        //监听行工具事件
        table.on('tool(content-list-filter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'remove':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/content/remove?id=' + data.id,
                            method: 'delete',
                            contentType: "application/json;charset=UTF-8",
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                        obj.del();
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
                case 'edit':
                    var index = layer.open({
                        type: 2
                        , title: '修改内容'
                        , content: '/cms/data/content/edit?id=' + data.id
                        , maxmin: false
                        , area: ['600px', '500px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-page-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/content/edit',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    layer.full(index);
                    break;
            }
        });

        var active = {
            search: function () {
                //执行重载
                tableIns.reload({
                    where: {
                        fieldName : fieldName.val(),
                        keyword : keyword.val(),
                        groupId : formgroup.val()
                    }
                });
            },
            addGroup: function () {
                layer.prompt({title: '添加内容分组'}, function(value, index){
                    $.ajax({
                        url: '/api/content/group/add?name=' + value + '&module='+ code,
                        method: 'post',
                        success: function (res) {
                            if (res.code == '0') {
                                layer.close(layer.index);
                                layer.msg('内容分组添加成功!', {icon: 6, shade: 0.2,time: 2000},function () {
                                    location.reload();
                                });
                            } else {
                                layer.msg(res.msg);
                            }
                        },
                        error: function (err) {
                        }
                    });
                });
            },
            removeGroup: function () {
                var id = formgroup.val();
                if (id == null || isNaN(id) || id.length == 0 ) {
                    layer.msg('请先选择内容分组!');
                } else {
                    $.ajax({
                        url: '/api/content/group/remove?id=' + id,
                        method: 'delete',
                        success: function (res) {
                            if (res.code == '0') {
                                layer.close(layer.index);
                                layer.msg('内容分组删除成功!', {icon: 6, shade: 0.2,time: 2000},function () {
                                    location.reload();
                                });
                            } else {
                                layer.msg(res.msg);
                            }
                        },
                        error: function (err) {
                        }
                    });
                }
            }
        };

        form.on('select(layui-form-group-filter)', function(data){
            tableIns.reload({
                where: {
                    fieldName : fieldName.val(),
                    keyword : keyword.val(),
                    groupId : formgroup.val()
                }
            });
        });

        $('.layui-form .layui-form-item .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>